package com.example.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class NotifyExampleService {

    private final Object lock = new Object();
    private boolean conditionMet = false;

    public void waitForCondition() {
        synchronized (lock) {
            while (!conditionMet) {
                try {
                    log.info("等待条件满足...");
                    lock.wait(); // 等待条件满足
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    log.error("线程被中断", e);
                }
            }
            log.info("条件满足，继续处理任务...");
        }
    }

    public void notifyCondition() {
        synchronized (lock) {
            conditionMet = true;
            log.info("条件已经满足，唤醒等待线程...");
            lock.notify(); // 唤醒等待线程
        }
    }
}
